
package com.azxc.rapid.plus.system.controller;

import com.azxc.rapid.core.secure.utils.AuthUtil;
import com.azxc.rapid.plus.system.dto.RoleConfigManagerDTO;
import com.azxc.rapid.system.user.cache.UserCache;
import com.azxc.rapid.system.user.feign.IUserClient;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import com.azxc.rapid.core.boot.ctrl.RapidController;
import com.azxc.rapid.core.mp.support.Condition;
import com.azxc.rapid.core.mp.support.Query;
import com.azxc.rapid.core.tool.api.R;
import com.azxc.rapid.core.tool.utils.Func;

import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.azxc.rapid.plus.system.entity.RoleConfigManager;
import com.azxc.rapid.plus.system.vo.RoleConfigManagerVO;
import com.azxc.rapid.plus.system.wrapper.RoleConfigManagerWrapper;
import com.azxc.rapid.plus.system.service.IRoleConfigManagerService;

import java.util.List;


/**
 * 角色管理 控制器
 * @author 91311
 * @since 2023-07-06
 */
@RestController
@AllArgsConstructor
@RequestMapping("roleConfigManager")
@Api(value = "角色管理", tags = "角色管理接口")
public class RoleConfigManagerController extends RapidController {

	private final IRoleConfigManagerService roleConfigManagerService;

	private final IUserClient userClient;
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入roleConfigManager")
	public R<RoleConfigManagerVO> detail(RoleConfigManager roleConfigManager) {
		RoleConfigManager detail = roleConfigManagerService.getOne(Condition.getQueryWrapper(roleConfigManager));
		return R.data(RoleConfigManagerWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 角色管理
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入roleConfigManager")
	public R<IPage<RoleConfigManagerVO>> list(RoleConfigManager roleConfigManager, Query query) {
		IPage<RoleConfigManager> pages = roleConfigManagerService.page(Condition.getPage(query), Condition.getQueryWrapper(roleConfigManager));
		return R.data(RoleConfigManagerWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 角色管理
	 */
	@GetMapping("/customerPage")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入roleConfigManager")
	public R<IPage<RoleConfigManagerVO>> page(RoleConfigManagerVO roleConfigManager, Query query) {
		IPage<RoleConfigManagerVO> pages = roleConfigManagerService.selectRoleConfigManagerPage(Condition.getPage(query), roleConfigManager);
		return R.data(pages);
	}

	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增-可分配角色", notes = "传入roleConfigManager")
	public R saveRoleRel(@Valid @RequestBody RoleConfigManagerDTO roleConfigManager) {
		return R.status(roleConfigManagerService.saveRoleRel(roleConfigManager));
	}

//	/**
//	 * 新增 角色管理
//	 */
//	@PostMapping("/save")
//	@ApiOperationSupport(order = 4)
//	@ApiOperation(value = "新增", notes = "传入roleConfigManager")
//	public R save(@Valid @RequestBody RoleConfigManager roleConfigManager) {
//		return R.status(roleConfigManagerService.save(roleConfigManager));
//	}

//	/**
//	 * 修改 角色管理
//	 */
//	@PostMapping("/update")
//	@ApiOperationSupport(order = 5)
//	@ApiOperation(value = "修改", notes = "传入roleConfigManager")
//	public R update(@Valid @RequestBody RoleConfigManager roleConfigManager) {
//		return R.status(roleConfigManagerService.updateById(roleConfigManager));
//	}


	/**
	 * 删除 角色管理
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(roleConfigManagerService.removeByIds(Func.toLongList(ids)));
	}

	@GetMapping("/getRoleListByMasterRoleIds")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "根据用户角色获取可分配的角色列表", notes = "多个角色id用逗号分割")
	public R<List<RoleConfigManagerVO>> getRoleListByMasterRoleId(String roleIds) {
		//不传获取当前用户的角色
		if(roleIds == null){
			roleIds =  UserCache.getUser(AuthUtil.getUserId()).getRoleId();
		}
		List<RoleConfigManagerVO> list = roleConfigManagerService.getRoleListByMasterRoleId(roleIds);
		return R.data(list);
	}

}
